package com.atguigu.cloud.mygateway;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
@Slf4j
public class MyGlobalFilter implements GlobalFilter, Ordered {
    public static final String BEGIN_VISIT_TIME = "begin_visit_time"; //开始调用代码的时间

    /**
     * @param exchange
     * @param chain
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 1.添加进来的时间和结束的时间
        exchange.getAttributes().put(BEGIN_VISIT_TIME,System.currentTimeMillis());
        return chain.filter(exchange).then(Mono.fromRunnable(()->{
            // 2.结束调用代码的时间
            long beginTime = exchange.getAttribute(BEGIN_VISIT_TIME);
            log.info("访问接口主机:" + exchange.getRequest().getURI().getHost());
            log.info("访问接口端口:" + exchange.getRequest().getURI().getPort());
            log.info("访问接口URL:" + exchange.getRequest().getURI().getPath());
            log.info("访问接口URL后面的参数:" + exchange.getRequest().getURI().getRawQuery());
            long endTime = System.currentTimeMillis();
            log.info("访问接口时长:" + "耗时："+(endTime-beginTime)+"ms");
            log.info("================================================================");
        }));
    }

    /**
     * 数字越小 优先级越高
     * @return
     */
    @Override
    public int getOrder() {
        return 0;
    }
}
